package cn.kgc.service.impl;

import cn.kgc.dao.AccountDao;
import cn.kgc.pojo.Account;
import cn.kgc.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;

@Service("accountService")
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;

    public Account getAccountByNum(String accountNum) {
        return accountDao.getAccountByNum(accountNum);
    }

    public Integer updateAccount(Account account) {
        return accountDao.updateAccount(account);
    }

    public void addAccount() {
        int i=1/0;
        System.out.println("保存账户成功");
    }

    public void delAccount(Integer id) {
        System.out.println("账户"+id+"删除成功");
    }

    /**
     * 转账
     * @param sourceNum
     * @param desNum
     * @param money
     */
    public void transfer(String sourceNum, String desNum, Float money) {
       Account sourceAccount=accountDao.getAccountByNum(sourceNum);
       Account desAccount=accountDao.getAccountByNum(desNum);

        sourceAccount.setAccountMoney(sourceAccount.getAccountMoney()-money);
        desAccount.setAccountMoney(desAccount.getAccountMoney()+money);

        accountDao.updateAccount(sourceAccount);
        int i=1/0;
        accountDao.updateAccount(desAccount);
    }
}
